import {DateUtil} from "baitu";

export default function (Vue) {
    Vue.mixin({
        methods: {
            addHistory(value, type) {
                if (!this.$store.getters.getOpenToolHistory) {
                    return;
                }
                type = type || this.$options.name;
                const historyList = this.getHistory(type);
                const data = {
                    value: value,
                    date: DateUtil.formatDateTime()
                }
                if (historyList.length > 30) {
                    historyList.pop();
                }
                historyList.unshift(data);
                localStorage.setItem(type + '-history-list', JSON.stringify(historyList));
            },
            getHistory(type) {
                type = type || this.$options.name;
                const res = localStorage.getItem(type + '-history-list');
                return res ? JSON.parse(res) : [];
            },
            setHistory(type, historyList = []) {
                type = type || this.$options.name;
                localStorage.setItem(type + '-history-list', JSON.stringify(historyList));
            }
        }
    })
}
